<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :fullscreen="true"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="作者id" prop="createUserId" hidden>
      <el-input v-model="dataForm.createUserId" placeholder="作者id"></el-input>
    </el-form-item>
    <el-form-item label="题目" prop="title">
      <el-input v-model="dataForm.title" placeholder="题目"></el-input>
    </el-form-item>
    <el-form-item label="文章类型" prop="articleType">
      <el-select v-model="dataForm.articleType" value-key="value" @change="selectChange"  placeholder="文章类型" value="">
        <el-option v-for="item in options"
                   :key="item.value"
                   :label="item.label"
                   :value="item.value"
                   :disabled="item.disabled">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime" hidden>
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime" hidden>
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="关联部门">
      <el-tag v-for="s in deptChange" :key="s" type="success">{{s}}</el-tag>
    </el-form-item>
    <el-form-item label="文章内容" prop="articleContent">
      <tinymce v-if="this.isRouterAlive" ref="editor" v-model="dataForm.articleContent" placeholder="文章具体内容" :height="300"/>
    </el-form-item>
    <el-form-item label="是否删除 0:未删除 1:已删除" prop="isDelete" hidden>
      <el-input v-model="dataForm.isDelete" placeholder="是否删除 0:未删除 1:已删除"></el-input>
    </el-form-item>
    <el-form-item label="" prop="def0" hidden>
      <el-input v-model="dataForm.def0" placeholder=""></el-input>
    </el-form-item>
      <el-form-item label="" prop="def1" hidden>
        <el-input v-model="dataForm.def0" placeholder=""></el-input>
      </el-form-item>
    <el-form-item label="" prop="def2" hidden>
      <el-input v-model="dataForm.def2" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="isLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import Tinymce from '@/components/Tinymce'

  export default {
    data () {
      return {
        visible: false,
        isRouterAlive: true,
        isLoading: false,
        dataForm: {
          id: 0,
          createUserId: this.$store.state.user.id,
          articleType: '',
          title: '',
          createTime: '',
          updateTime: '',
          articleContent: '',
          isDelete: '',
          def0: this.$store.state.user.name,  // 用户名
          def1: '',                           // 关联部门
          def2: ''
        },
        deptChange: [],
        options: [{
          value: '0',
          label: '农业产出'
        }, {
          value: '1',
          label: '生产能力'
        }, {
          value: '2',
          label: '绿色发展'
        }, {
          value: '3',
          label: '乡村建设'
        }, {
          value: '4',
          label: '产业发展'
        }, {
          value: '5',
          label: '农村改革'
        }],
        dataRule: {
          articleType: [
            { required: true, message: '文章类型不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '题目不能为空', trigger: 'blur' }
          ],
          articleContent: [
            { required: true, message: '文章具体内容不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      Tinymce
    },
    computed: {
      businessDepts: {
        get () { return this.$store.state.dept.businessDepts },
        set (val) { this.$store.commit('dept/upodateBusinessDepts', val) }
      }
    },
    methods: {
      selectChange (key) {
        this.deptChange = []
        this.businessDepts.forEach(item => {
          if (item.businessId === +key) {
            this.deptChange.push(item.name)
          }
        })
      },
      reload () {
        this.isRouterAlive = false
        this.$nextTick(() => (this.isRouterAlive = true))
      },
      init (id) {
        this.dataForm.id = id || 0
        this.isLoading = false
        this.deptChange = ['未关联']
        this.reload()
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/businesswork/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createUserId = data.businessWork.createUserId
                this.dataForm.articleType = data.businessWork.articleType.toString()
                this.dataForm.title = data.businessWork.title
                this.dataForm.createTime = data.businessWork.createTime
                this.dataForm.updateTime = data.businessWork.updateTime
                this.dataForm.articleContent = data.businessWork.articleContent
                this.dataForm.isDelete = data.businessWork.isDelete
                this.dataForm.def0 = data.businessWork.def0
                this.dataForm.def1 = data.businessWork.def1
                this.dataForm.def2 = data.businessWork.def2
                this.selectChange(this.dataForm.articleType)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.isLoading = true
            this.$http({
              url: this.$http.adornUrl(`/business/businesswork/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'articleType': this.dataForm.articleType,
                'title': this.dataForm.title,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'articleContent': this.dataForm.articleContent,
                'isDelete': this.dataForm.isDelete,
                'def0': this.dataForm.def0,
                'def1': this.deptChange.join(','),
                'def2': this.dataForm.def2
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
